<?php

/*
* Adds a user to the members table. This insertion associates the 
* user with a proejct that he is a member of. 
*
* @method post
* @header pname The project name 
* @header fname The user's name
* @header chicken true or false, depending on whether the 'chicken_check'
*         chickbox was clicked.
*
* @return 'User does not exist'
*          If the username is not found in the members table
* @return 'User is already in that project'
*          If the username is already associated with that project in
*          the projects table
* @return html containing participants
*          If there is no error and the user is successfully inserted
*          into the projects table
*/

require_once("connect_to_database.php");

// User INFORMATION.... why is it called fname??
$participant = mysql_real_escape_string($_POST['fname']);
$project_name = mysql_real_escape_string($_POST['pname']);
$chicken = mysql_real_escape_string($_POST['chicken']) == "true" ? true : false ;

//check to see if the user exists
$query = sprintf("SELECT username FROM members WHERE username='%s'", $participant);
$result = mysql_query($query);

if(!mysql_num_rows($result))
	die("User does not exist");
			
//insert the user into the members table with the project
$query = sprintf("INSERT INTO projects(name,participant, isChicken ) values('%s','%s' %s);",
	$project_name, $participant,
	$chicken ? ",1" : ",0");
$result = mysql_query($query);

if(!$result)
	die("User is already in that project");



//return the new list of participants in html form, ready to be inserted into the 
// #users div
include('get_participants.php');
?>
